﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using HostsManager.Model;

namespace HostsManager.Controls
{
    internal partial class TreeControl : UserControl
    {
        internal delegate void DataChangedEventHandler(object sender);
        internal event DataChangedEventHandler DataChanged;


        internal delegate void NewSectionEventHandler(object sender);
        internal event NewSectionEventHandler NewSection;


        internal delegate void RenameSectionEventHandler(Section section);
        internal event RenameSectionEventHandler RenameSection;

        internal delegate void RemoveSectionEventHandler(Section section);
        internal event RemoveSectionEventHandler RemoveSection;


        internal delegate void AddConfigurationEventHandler(Section section);
        internal event AddConfigurationEventHandler AddConfiguration;

        internal delegate void EditConfigurationEventHandler(Configuration configuration);
        internal event EditConfigurationEventHandler EditConfiguration;

        internal delegate void RenameConfigurationEventHandler(Configuration configuration);
        internal event RenameConfigurationEventHandler RenameConfiguration;

        internal delegate void RemoveConfigurationEventHandler(Configuration configuration);
        internal event RemoveConfigurationEventHandler RemoveConfiguration;

        public TreeControl()
        {
            InitializeComponent();

            flowLayoutPanel1.AutoScroll = true;
        }

        internal void ShowHosts(Hosts hosts)
        {
            flowLayoutPanel1.Controls.Clear();

            if (hosts == null)
                return;

            foreach (Section section in hosts.Sections.Values)
            {
                if (section != hosts.DefaultSection)
                {
                    SectionControl s = new SectionControl(section);
                    s.SectionEnabled = section.Enabled;

                    s.DataChanged += new SectionControl.DataChangedEventHandler(OnDataChanged);

                    s.RenameSection += new SectionControl.RenameSectionEventHandler(OnRenameSection);
                    s.RemoveSection += new SectionControl.RemoveSectionEventHandler(OnRemoveSection);

                    s.AddConfiguration += new SectionControl.AddConfigurationEventHandler(OnAddConfiguration);
                    s.EditConfiguration += new SectionControl.EditConfigurationEventHandler(OnEditConfiguration);
                    s.RenameConfiguration += new SectionControl.RenameConfigurationEventHandler(OnRenameConfiguration);
                    s.DeleteConfiguration += new SectionControl.DeleteConfigurationEventHandler(OnDeleteConfiguration);

                    s.Visible = true;
                    flowLayoutPanel1.Controls.Add(s);
                }
            }
        }

        private void newSectionButton_Click(object sender, EventArgs e)
        {
            OnNewSection(sender);
        }

        void OnRenameSection(Section section)
        {
            if (RenameSection != null)
                RenameSection(section);
        }

        void OnRemoveSection(Section section)
        {
            if (RemoveSection != null)
                RemoveSection(section);
        }


        void OnAddConfiguration(Section section)
        {
            if (AddConfiguration != null)
                AddConfiguration(section);
        }

        protected void OnEditConfiguration(Configuration configuration)
        {
            if (EditConfiguration != null)
                EditConfiguration(configuration);
        }

        protected void OnRenameConfiguration(Configuration configuration)
        {
            if (RenameConfiguration != null)
                RenameConfiguration(configuration);
        }

        protected void OnDeleteConfiguration(Configuration configuration)
        {
            if (RemoveConfiguration != null)
                RemoveConfiguration(configuration);
        }

        protected void OnDataChanged(object sender)
        {
            if (DataChanged != null)
                DataChanged(sender);
        }

        protected void OnNewSection(object sender)
        {
            if (NewSection != null)
                NewSection(sender);
        }
    }
}
